const mongoose = require("mongoose");

const ArticleSchema = new mongoose.Schema({
  author: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "User", // 关联作者
    required: true,
  },
  title: {
    type: String,
    required: true,
    trim: true,
  },
  coverImage: {
    type: String,
    default: "",
  },
  summary: {
    type: String,
    default: "",
  },
  content: {
    type: String,
    required: true,
  },
  category: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Category", // 关联多个分类
    },
  ],
  likes: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User", // 存储点赞人的 ID
    },
  ],
  collections: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User", // 存储点赞人的 ID
    },
  ],
  comments: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User", // 存储评论人的 ID
    },
  ],
  createdAt: {
    type: Date,
    default: Date.now,
  },
});

// 删除敏感信息
ArticleSchema.set("toJSON", {
  transform(doc, ret) {
    ret.id = ret._id;
    delete ret._id;
    delete ret.__v;
    return ret;
  },
});

module.exports = mongoose.model("Article", ArticleSchema);
